519. 随机翻转矩阵
为保证权益,题目请参考 519. 随机翻转矩阵(From LeetCode).
解决方案1
Python
python
# 519. 随机翻转矩阵
# https://leetcode-cn.com/problems/random-flip-matrix/
################################################################################
import random
from typing import List
class Solution:
def __init__(self, m: int, n: int):
self.m = m
self.n = n
self.r = m * n
self.map = {}
def flip(self) -> List[int]:
t = random.randint(0, self.r - 1)
tc = t
while tc in self.map:
tc = self.map[tc]
i = tc // self.n
j = tc % self.n
self.map[t] = self.r - 1
self.r -= 1
return [i, j]
def reset(self) -> None:
self.r = self.m * self.n
self.map.clear()
# Your Solution object will be instantiated and called as such:
# obj = Solution(m, n)
# param_1 = obj.flip()
# obj.reset()
################################################################################
if __name__ == '__main__':
solution = Solution(10, 10)
for i in range(100):
print(solution.flip())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47